package lev01_beginnerClass.class06_binarySortFind;


import lev01_beginnerClass.util.HelpUtil;

import java.util.Arrays;

/**
 * find a target num in a ordered array
 */
public class L01_findTargetInOrderArray {


    public static void main(String[] args) {
        for(int i = 0 ;i< 100 ;i++){
            int[] arr = HelpUtil.generateRandomArr(10, 10);
            Arrays.sort(arr);
            System.out.println(isExist(arr,5)+"     "+findTarget(arr,5));
        }
    }
    public static boolean findTarget(int[] arr,int target){
        //give priority to boundary condition
        if(arr == null|| arr.length ==0){
            return false;
        }

        int left = 0;int right = arr.length-1;
        while (left <= right){
            int mid = left+(right-left)/2;
            if(arr[mid] == target){
                return true;
            } else if(arr[mid] > target){
                right = mid -1;
            }else{
                left = mid+1;
            }
        }
        return  false;
    }

    public static boolean isExist(int[] arr,int target){
        for(int i : arr){
            if(i == target){
                return true;
            }
        }
        return false;
    }
}
